<?php
// 广告主管理
namespace Admin\Controller;
use Think\Controller;
class FinanceController extends Controller{

	//操作日志结算记录
	public function  SettlementRecode(){
		//$this->isAjaxLogin();

		$model=M('settlement_order');
		$page=I('post.page/d');
		$page_size=I('post.page_size/d');
		$start_time=I('post.start_time');
		$end_time=I('post.end_time');
		$channel_id=I('post.channel_id');

		 if (!empty($start_time)&&!empty($end_time)) {

        	$where['add_time']  = [['egt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }
         if(!empty($channel_id)) {
            $where['channel_id'] = $channel_id;
        }

		$count=$model->where($where)->count();
		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;
		$where['balance_status']="2";
		
		$result=$model->where($where)->limit($limitStart,$page_size)->select();


		
		$result=array_map(function($val){
			$val['generation_time']=date("Y-m-d",$val['generation_time']);
			return $val;
		}, $result);
		

		

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data']['list'] = $result;
            $ajaxReturn['data']['total_page'] = $count;

        
        $this->ajaxReturn($ajaxReturn);
	}


	public function  SettlementOrderList(){

		//$this->isAjaxLogin();

		$model=M('settlement_order');
		$page=I('post.page/d');
		$page_size=I('post.page_size/d');
		$start_time=I('post.start_time');
		$end_time=I('post.end_time');

		 if (!empty($start_time)&&!empty($end_time)) {

        	$where['add_time']  = [['egt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }

		$count=$model->where($where)->count();
		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;
		
		
		$result=$model->limit($limitStart,$page_size)->where($where)->select();
		
		$result=array_map(function($val){
			$val['generation_time']=date("Y-m-d",$val['generation_time']);
			return $val;
		}, $result);
		

		 

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data']['list'] = $result;
            $ajaxReturn['data']['total_page'] = $count;

       

        $this->ajaxReturn($ajaxReturn);
	}
	//是否付款
	public function PaymentComment(){
		//$this->isAjaxLogin();

		$id=I('post.id/d');
		$model=M('settlement_order');
		$data['pay_status']="1";
		$result=$model->where([
				'id'=>$id,
				'balance_status'=>"0"
			])->save($data);
		if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";

        }else{

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg'] = "失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	//是否收到发票
	public function ConfirmInvoice(){

		//$this->isAjaxLogin();

		$id=I('post.id');
		$model=M('settlement_order');
		$data['invoice_status']="1";
		$data['balance_status']="2";
		$result=$model->where([
				'id'=>$id,
				'balance_status'=>"0",
				"pay_status"=>"1"
			])->save($data);

		$resultid=$model->field('channel_data_id_list')->where('id='.$id)->find();
		$resultid=$resultid['channel_data_id_list'];
		$resultid=explode(',', $resultid);

		foreach ($resultid as $k => $v) {
			$modeldata=M('channel_data');
			$dataas['balance_status']="2";
			$modeldata->where('id='.$v)->save($dataas);
		}
		if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg'] = "失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	//财务打回发票
	public function  QuxiaoChannelOrder(){
		//$this->isAjaxLogin();
		
		$id=I('post.id');
		$modelsettle=M('settlement_order');
		 $datas['balance_status']="1";
		 $results=$modelsettle->where([
		 		'id'=>$id,
		 		'balance_status'=>"0"
		 	])->save($datas);

		$resultid=$modelsettle->field('channel_data_id_list')->where('id='.$id)->find();
		$resultid=$resultid['channel_data_id_list'];
		$resultid=explode(',', $resultid);

		foreach ($resultid as $k => $v) {
			$modeldata=M('channel_data');
			$dataas['balance_status']="0";
			$modeldata->where('id='.$v)->save($dataas);
		}

		if($results){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";

        }else{

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg'] = "失败！";
        }

        $this->ajaxReturn($ajaxReturn);
		
	}
}